<?php

class Validate {
  const SUCCESS = 1;
  const WARNING = 0;
  const ERROR = -1;
  const EXCEPTION = -2;

  public static function isValid($rules, $value) {
    $msgs = array();
    $result = Validate::SUCCESS;

    foreach ($rules as $index => $name) {
      list($class_name, $params) = self::_inflect($index, $name);
      $checker = new $class_name($params);
      if (($msg = $checker->isValid($value)) != true) {
        $msgs[] = $checker->getMessage($value);
        $level = $checker->getLevel();

        if ($level < $result) {
          $result = $level;
        }
        
        if ($result == Validate::EXCEPTION) {
          return array($result, $msgs);
        }
      }
    }
    return array($result, $msgs);
  }

  protected static function _inflect($index, $name) {

    if (is_int($index)) {
      $class_name = $name;
      $params = array();
    } else {
      $class_name = $index;
      $params = (array) $name;
    }
    //$class_name = 'Validate_' . Core::inflect($class_name);
    return array($class_name, $params);
  }

}